package com.project2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.po.MarketAd;
import com.project2.bean.po.MarketAdExample;
import com.project2.bean.vo.AdminPageInfo;
import com.project2.mapper.MarketAdMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Frank Zhu
 */
@Service
@Transactional
public class AdminAdServiceImpl implements AdminAdService {

    @Autowired
    MarketAdMapper marketAdMapper;

    /**
     * 广告回显,应该显示所有未被删除的，即deleted=0的
     * @author Frank Zhu
     */
    @Override
    public CommonData getAllAds(PageBo pageInfo, String name, String content) {
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getLimit());
        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        // 统一增加检索顺序，根据add_time降序排序
        marketAdExample.setOrderByClause(pageInfo.getSort() + " " + pageInfo.getOrder());
        // 统一增加限定条件，只检索没被删除的
        criteria.andDeletedEqualTo(false);
        List<MarketAd> marketAds = null;

        if (name == null && content == null) {
            // 默认之查找未被删除的
            marketAds = marketAdMapper.selectByExample(marketAdExample);
        }else if(name != null && content == null){
            // 使用模糊查询
            criteria.andNameLike("%" + name + "%");
            marketAds = marketAdMapper.selectByExample(marketAdExample);

        }else if(name == null){
            criteria.andContentLike("%" + content + "%");
            marketAds = marketAdMapper.selectByExample(marketAdExample);
        }else {
            criteria.andNameLike("%" + name + "%");
            criteria.andContentLike("%" + content + "%");
            marketAds = marketAdMapper.selectByExample(marketAdExample);
        }

        PageInfo<MarketAd> info = new PageInfo<>(marketAds);

        return CommonData.data(info);
    }

    /**
     * 创建新的广告信息
     * @return
     */
    @Override
    public Boolean createAd(MarketAd newAd) {
        newAd.setAddTime(newAd.getUpdateTime());
        int num = marketAdMapper.insertSelective(newAd);
        if(num != 1){
            return false;
        }
        return true;
    }

    /**
     * 更新广告信息
     * @param ad
     * @return 将数据库更新后的信息返回给前端
     */
    @Override
    public MarketAd updateAd(MarketAd ad) {

        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        criteria.andIdEqualTo(ad.getId());

        ad.setUpdateTime(new Date());

        int i = marketAdMapper.updateByExampleSelective(ad, marketAdExample);

        if(i != 1){
            return null;
        }

        MarketAd marketAd = marketAdMapper.selectByPrimaryKey(ad.getId());
        return marketAd;
    }

    /**
     * 删除广告信息，只需要将deleted和updateTime更新即可
     * @param id 指定的广告的id
     * @return
     */
    @Override
    public Boolean deleteAd(Integer id) {

        MarketAd deleteInfo = new MarketAd();
        // 逻辑删除位设置为true
        deleteInfo.setDeleted(true);
        // 更新更新时间
        deleteInfo.setUpdateTime(new Date());

        MarketAdExample marketAdExample = new MarketAdExample();
        marketAdExample.createCriteria().andIdEqualTo(id);

        int i = marketAdMapper.updateByExampleSelective(deleteInfo, marketAdExample);

        if(i != 1){
            // 如果出问题，进行事务的回滚
            return false;
        }

        return true;
    }
}
